﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using SharpDB.Entity;
using SharpDB.SPI;
using SharpDB;
using System.Collections.Concurrent;
using System.Text.RegularExpressions;
using System.Data.SqlClient;

namespace SharpDB.SqlServer
{
    public partial class SqlServerDB : IDB
    {
        /// <summary>
        /// SqlServerDB 实例
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        public SqlServerDB(string connectionString)
        {
            this.ConnectionString = connectionString;
            this.Info = new DBInfo(this);
        }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string ConnectionString
        {
            get; private set;
        }

        /// <summary>
        /// 数据库相关信息
        /// </summary>
        public DBInfo Info
        {
            get; private set;
        }

        /// <summary>
        /// 定时器 查询表结构的 间隔（单位：毫秒）
        /// </summary>
        public double TimerInterval
        {
            get
            {
                if (Info != null && Info.timer != null)
                    return Info.timer.Interval;
                else
                    return 0;
            }
        }

        /// <summary>
        /// 等待命令执行所需的时间（以秒为单位）。 默认值为 30 秒。
        /// </summary>
        private int _CmdTimeOut = 30;
        public int CmdTimeOut
        {
            set { _CmdTimeOut = value; }
            get { return _CmdTimeOut; }
        }


        /// <summary>
        /// 异常前 执行的事件。
        /// 意义/作用：在报异常前记录 执行的Sql命令参数 ，以及异常信息，类似于 飞机黑匣子。
        /// </summary>

        public event EventHandler<OnErrorEventArgs> OnError;

        
        
        private void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction trans, string cmdText, IDataParameter[] cmdParms, int timeout, CommandType cmdType = CommandType.Text)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
                cmd.Transaction = trans;
            cmd.CommandType = cmdType;
            if (timeout > 0)
            {
                cmd.CommandTimeout = timeout;
            }
            else
            {
                cmd.CommandTimeout = CmdTimeOut;
            }
            if (cmdParms != null && cmdParms.Length > 0)
            {
                foreach (IDataParameter parameter in cmdParms)
                {
                    if ((parameter.Direction == ParameterDirection.InputOutput
                        || parameter.Direction == ParameterDirection.Input)
                        && (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(parameter);
                }
            }
        }


    }
}
